package android.slc.base.api.slc;

import android.slc.box.or.SlcNu;
import android.slc.base.api.slc.config.ApiConfig;
import android.slc.base.api.slc.converter.SlcConverterFactory;
import android.slc.base.api.slc.interceptor.TokenInterceptor;
import android.slc.base.repository.local.sp.AppConfig;
import android.slc.module.component.attchment.utils.AttachmentConfig;
import cn.hutool.log.Log;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.apache.commons.lang3.StringUtils;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;

public class SlcApi {
    public static void init() {
        HttpLoggingInterceptor httpLoggingInterceptor = null;
        if (!AppConfig.isIsRelease()) {
            httpLoggingInterceptor = new HttpLoggingInterceptor((String message) -> Log.get().debug("OkHttp:" + message));
            httpLoggingInterceptor.level(HttpLoggingInterceptor.Level.BODY);
        }
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .readTimeout(12, TimeUnit.SECONDS)
                .writeTimeout(12, TimeUnit.SECONDS)
                .connectTimeout(12, TimeUnit.SECONDS)
                .addNetworkInterceptor(new TokenInterceptor());
        if (httpLoggingInterceptor != null) {
            builder.addNetworkInterceptor(httpLoggingInterceptor);
        }
        SlcNu.getInstance().setDefRetrofit(new Retrofit.Builder()
                .baseUrl(ApiConfig.getInstance().getServiceApiAddress())
                .client(builder.build())
                .addConverterFactory(SlcConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build());
        //OkDownload.Builder okDownloadBuilder =new OkDownload.Builder(context).connectionFactory(new DownloadOkHttp3ConnectionFixBug.Factory());
        //OkDownload.setSingletonInstance(okDownloadBuilder.build());

        AttachmentConfig.getInstance().setDownloadIpPort(ApiConfig.getInstance().getServiceApiAddress());
        AttachmentConfig.getInstance().setDownloadApiPart("common/download/resource?resource=");

    }

    private static String getIpAddress() {
        String baseUrl = ApiConfig.getInstance().getServiceApiAddress();
        return getIpAddress(baseUrl.substring(7, baseUrl.length() - 9));
    }

    private static String getIpAddress(String prefix) {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                }
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = addresses.nextElement();
                    if (ip instanceof Inet4Address) {
                        if (StringUtils.contains(ip.getHostAddress(), prefix)) {
                            return ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("IP地址获取失败" + e.toString());
        }
        return "";
    }
}
